package com.shtel.customerasset.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shtel.customerasset.entity.*;
import com.shtel.customerasset.mapper.QueryMapper;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerassetService {
    @Autowired
    QueryMapper queryMapper;

    //查询在用套餐
    public List<UsingTaocan> querytaocans(String creditnum) {
        List<UsingTaocan> usingTaocans = new ArrayList<>();
        List<String> taocanids = queryMapper.queryuseingtaocan(creditnum);
        System.out.println("测试:"+taocanids.toString());
        System.out.println("查询在用套餐" + taocanids.size());
        for (String taocanid : taocanids) {
            Taocan taocan = new Taocan();
            System.out.println("套餐id"+taocanid);
            taocan = queryMapper.querytaocaninfo(taocanid);
            Custyewuinfo custyewuinfo =queryMapper.queryuseingtaocanstartandendtime(creditnum,taocanid);
            System.out.println("测试:"+custyewuinfo.getId());
            System.out.println("测试:"+custyewuinfo.getTaocan_starttime());
            UsingTaocan usingTaocan = new UsingTaocan(taocan);

            usingTaocan.setTaocan_starttime(custyewuinfo.getTaocan_starttime());
            usingTaocan.setTaocan_endtime(custyewuinfo.getTaocan_endtime());
            usingTaocan.setShebeinum(queryMapper.queryshebeinum(creditnum));
            usingTaocans.add(usingTaocan);
        }
        return usingTaocans;

    }

    //查询在用产品
    public List<UsingChanping> querychanpings(String creditnum, Integer num) {
        List<UsingChanping> usingChanpings = new ArrayList<>();
//        Integer pageNum = num;
//        Integer pageSize = 5;
//        Page page = PageHelper.startPage(pageNum, pageSize, true);
        List<String> chanpingids = queryMapper.queryuseingchanping(creditnum);
        List<String> taocanids = queryMapper.queryuseingtaocan(creditnum);
        Map<String,List<String>> taocanneichanpingmap= new HashMap<>();
        for(String taocanid :taocanids){
            List<String> taocanneichanpingids= new ArrayList<>();
            Taocan taocan = queryMapper.querytaocaninfo(taocanid);
            if(!(taocan.getChanping1()==null)){
                taocanneichanpingids.add(taocan.getChanping1());
            }
            if(!(taocan.getChanping2()==null)){
                taocanneichanpingids.add(taocan.getChanping2());
            }
            if(!(taocan.getChanping3()==null)){
                taocanneichanpingids.add(taocan.getChanping3());
            }
            if(!(taocan.getChanping4()==null)){
                taocanneichanpingids.add(taocan.getChanping4());
            }
            if(!(taocan.getChanping5()==null)){
                taocanneichanpingids.add(taocan.getChanping5());
            }
            if(!(taocan.getChanping6()==null)){
                taocanneichanpingids.add(taocan.getChanping6());
            }
            if(!(taocan.getChanping7()==null)){
                taocanneichanpingids.add(taocan.getChanping7());
            }
            if(!(taocan.getChanping8()==null)){
                taocanneichanpingids.add(taocan.getChanping8());
            }
            taocanneichanpingmap.put(taocanid,taocanneichanpingids);

        }

        for (String chanpingid : chanpingids) {
            Chanping chanping = new Chanping();
            chanping = queryMapper.querychanpinginfo(chanpingid);
            UsingChanping usingChanping = new UsingChanping(chanping);
            Custyewuinfo custyewuinfo = queryMapper.queryuseingchanpingstartandendtime(creditnum,chanpingid);
            usingChanping.setChanping_starttime(custyewuinfo.getChanping_starttime());
            usingChanping.setChanping_endtime(custyewuinfo.getChanping_endtime());
            usingChanping.setShebeinum(queryMapper.queryshebeinum(creditnum));
            usingChanpings.add(usingChanping);
        }

        for(Map.Entry<String,List<String>> entry:taocanneichanpingmap.entrySet()){
            for(String taocanneichanpingids:entry.getValue()){
                Chanping chanping = new Chanping();
                chanping = queryMapper.querychanpinginfo(taocanneichanpingids);
                UsingChanping usingChanping = new UsingChanping(chanping);
                System.out.println("测试"+entry.getKey());
                Custyewuinfo custyewuinfo = queryMapper.queryuseingtaocanstartandendtime(creditnum,entry.getKey());
                System.out.println("测试"+custyewuinfo.getId());
                System.out.println("测试"+custyewuinfo.getTaocan_starttime());
                usingChanping.setChanping_starttime(custyewuinfo.getTaocan_starttime());
                usingChanping.setChanping_endtime(custyewuinfo.getTaocan_endtime());
                usingChanping.setShebeinum(queryMapper.queryshebeinum(creditnum));
                usingChanpings.add(usingChanping);
            }
        }

        return usingChanpings;
    }

    //查询在途套餐
    public List<OnGoingTaoCan> queryongoingtaocans(String creditnum) {
//        List<Taocan> taocans = new ArrayList<>();
//        List<String> taocanids = queryMapper.queryongoingtaocan(creditnum);
//        for (String taocanid : taocanids) {
//            Taocan taocan = new Taocan();
//            taocan = queryMapper.querytaocaninfo(taocanid);
//            taocans.add(taocan);
//        }


        return queryMapper.newqueryongoingtaocan(creditnum);

    }

    //查询在途产品
    public List<Chanping> queryongoingchanpings(String creditnum) {
        List<Chanping> chanpings = new ArrayList<>();
        List<String> chanpingids = queryMapper.queryongoingchanping(creditnum);
        for (String chanpingid : chanpingids) {
            Chanping chanping = new Chanping();
            chanping = queryMapper.querychanpinginfo(chanpingid);
            chanpings.add(chanping);
        }
        return chanpings;
    }


    //查询终端信息
    public Endduan queryendduan(String creditnum) {

        return queryMapper.queryendduan(creditnum);

    }

    // 查询指标规则
    // public List<AlarmRule> queryIndexRule (int num){
    // Map<String, Object> returnData = new HashMap<>();
    //
    // Integer pageNum = num;
    // Integer pageSize = 5;
    // Page page = PageHelper.startPage(pageNum, pageSize, true);
    // List<AlarmRule> resultData = queryInfoMapper.queryIndexRule();
    //
    // return resultData;
    //
    // }

}
